﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

[CustomEditor(typeof(UpdateSpriteZ)), CanEditMultipleObjects]
public class UpdateSpriteZEditor : Editor
{
    private SerializedObject obj;
    UpdateSpriteZ targetobj;
    void OnEnable()
    {
        targetobj = target as UpdateSpriteZ;
        obj = new SerializedObject(target);
    }
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        obj.Update();

        if (GUILayout.Button("刷新layerZ脚本")) {
            AddLayerZScript();
        }

        obj.ApplyModifiedProperties();
    }

    private void AddLayerZScript()
    {
        SpriteRenderer[] srs = targetobj.GetComponentsInChildren<SpriteRenderer>();
        foreach (SpriteRenderer sr in srs)
        {
            sr.gameObject.AddComponentSafe<LayerZ>();
        }
        LayerZ[] layerzs = targetobj.GetComponentsInChildren<LayerZ>();
        targetobj.layerZs = layerzs;
    }
}

